package com.avic.rong.goodsSort.fragment.bz;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.api.GoodsSortApi;
import com.avic.rong.goodsSort.bean.GoodsSortBean;
import com.avic.rong.goodsSort.fragment.bz.secondfragment.GoodsBiaoZhunBZFragment;
import com.avic.rong.goodsSort.fragment.bz.secondfragment.GoodsBiaoZhunZBFragment;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 标准分类
 */
public class GoodsBiaoZhunFragment extends BaseFragment {

    @BindView(R.id.goods_Bz_Img)
    ImageView goodsBzImg;
    @BindView(R.id.goods_Bz_Tb)
    TabLayout goodsBzTb;
    @BindView(R.id.goods_Bz_Vp)
    ViewPager goodsBzVp;
    private List<String> titleList = new ArrayList<>();
    private BiaoZhunPagerAdapter pagerAdapter;
    private GoodsSortBean bean;
    private List<Fragment> fragmentList = new ArrayList<>();


    public GoodsBiaoZhunFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_biaozhun, container, false);
        ButterKnife.bind(this, view);
        initData();
        goodsBzVp.setOffscreenPageLimit(3);
        GoodsSortApi goodsSortApi = new GoodsSortApi((GoodsSortActivity) getContext(), delCartListener, 1, "bz");
        goodsSortApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsSortApi);
        return view;
    }



    //获取分类数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            bean = (GoodsSortBean) subjects.getData();
            Logger.e("cjx", "bz拿到数据");
            Glide.with(getActivity()).load(bean.getContent().getContent())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                    .into(goodsBzImg);

        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };


    private void initData() {
        titleList.add("标准类型分类");
        titleList.add("中标分类");
        titleList.add("国际标准分类");
        titleList.add("GJB 832A-2005");
        titleList.add("GJB 832-90");

        fragmentList.add(new GoodsBiaoZhunBZFragment());
        fragmentList.add(GoodsBiaoZhunZBFragment.getInstance("zb"));//中标分类
        fragmentList.add(GoodsBiaoZhunZBFragment.getInstance("gj"));//国际标准
        fragmentList.add(GoodsBiaoZhunZBFragment.getInstance("gjb05"));//国际标准
        fragmentList.add(GoodsBiaoZhunZBFragment.getInstance("gjb90"));//国际标准
//        fragmentList.add(new GoodsBiaoZhunGJBFragment());
//        fragmentList.add(new GoodsBiaoZhunGJBFragment());
        pagerAdapter = new BiaoZhunPagerAdapter(getChildFragmentManager());
        goodsBzVp.setAdapter(pagerAdapter);
        goodsBzTb.setupWithViewPager(goodsBzVp);

//        goodsBzTb.setViewPager(goodsBzVp,titleArray);

    }



    public class BiaoZhunPagerAdapter extends FragmentPagerAdapter {

        public BiaoZhunPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragmentList.get(i);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }
}
